package com.quanyan.financialSystem.service.place.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.financialSystem.entity.db.*;
import com.quanyan.financialSystem.entity.vo.req.ReqPlaceInComeStream;
import com.quanyan.financialSystem.entity.vo.resp.RespPlaceInComeStream;
import com.quanyan.financialSystem.mapper.*;
import com.quanyan.financialSystem.service.place.PlaceInComeStreamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * Created by Zj on 2016/10/17.
 * 场地收入流水
 */
@Service
public class PlaceInComeStreamServiceImpl implements PlaceInComeStreamService {
    private static final Logger logger = LoggerFactory.getLogger(PlaceInComeStreamServiceImpl.class);

    @Autowired
    private PlaceInComeStreamMapper placeInComeStreamMapper;


    @Autowired
    private TbBizPlaceBuyContractMapper tbBizPlaceBuyContractMapper;


    @Autowired
    private TbBizPlacePayItemMapper tbBizPlacePayItemMapper;






    @Override
    public APIResponse<PageObj<List<RespPlaceInComeStream>>> findAllPlaceInComeStream(ReqPlaceInComeStream reqPlaceInComeStream) {
        logger.info("开吃查询场地收入流水----》{}",reqPlaceInComeStream.toString());
        Page<RespPlaceInComeStream> page = new Page<RespPlaceInComeStream>();
        int pageIndex = reqPlaceInComeStream.getPageNo();
        page.setCurrentPage(pageIndex);
        page.setShowCount(reqPlaceInComeStream.getPageSize());
        List<RespPlaceInComeStream> lstResult ;
        if(reqPlaceInComeStream.getPayType() == null || reqPlaceInComeStream.getPayType().intValue() == 99){//表示全部
             lstResult = placeInComeStreamMapper.findAllPlaceInComeStream(page,reqPlaceInComeStream);
        }else{
             lstResult = placeInComeStreamMapper.findAllPlaceInComeStreamByPayItem(page,reqPlaceInComeStream);
        }


        if(lstResult != null && lstResult.size() > 0){
            List<Integer> lstPlaceId = new ArrayList<>();// 场馆id 用于查询购买方式
            List<String> lstOrderId = new ArrayList<>();//订单id
            for (RespPlaceInComeStream respPlaceInComeStream : lstResult){
                lstOrderId.add(respPlaceInComeStream.getPlaceOrderId());
                if(!lstPlaceId.contains(respPlaceInComeStream.getPlaceId().intValue()))
                    lstPlaceId.add(respPlaceInComeStream.getPlaceId());
            }

            /***查询支付方式 ***/
            TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
            TbBizPlacePayItemExample.Criteria payItemCriteria = tbBizPlacePayItemExample.createCriteria();
            payItemCriteria.andPlaceOrderIdIn(lstOrderId);
            List<TbBizPlacePayItem> payItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);



            /***查询购买方式 ***/
            TbBizPlaceBuyContractExample tbBizPlaceBuyContractDetailExample = new TbBizPlaceBuyContractExample();
            TbBizPlaceBuyContractExample.Criteria buyDetailCriteria = tbBizPlaceBuyContractDetailExample.createCriteria();
            buyDetailCriteria.andPlaceIdIn(lstPlaceId);
            buyDetailCriteria.andStatusEqualTo(2); //2表示合同正在执行
            List<TbBizPlaceBuyContract> lstPlaceBuyContractDetail = tbBizPlaceBuyContractMapper.selectByExample(tbBizPlaceBuyContractDetailExample);

            for (RespPlaceInComeStream respPlaceInComeStream : lstResult){
                /***查询购买方式 ***/
                for(TbBizPlaceBuyContract tbBizPlaceBuyContractDetail : lstPlaceBuyContractDetail){
                    if(respPlaceInComeStream.getPlaceId().intValue() == tbBizPlaceBuyContractDetail.getPlaceId().intValue()){
                        String tmpBuyType = respPlaceInComeStream.getBuyType() +tbBizPlaceBuyContractDetail.getBuyType() +"/";
                        respPlaceInComeStream.setBuyType(tmpBuyType);
                    }
                }

                /***查询支付方式 ***/
                for(TbBizPlacePayItem payItem : payItemList){
                    if(payItem.getPlaceOrderId().equals(respPlaceInComeStream.getPlaceOrderId())){
                        if (payItem.getPayType().intValue() == 7 || payItem.getPayType().intValue() == 10){ // 优惠方式
                            String tempPre = respPlaceInComeStream.getPreferentialType() + payItem.getPayType() + "/";
                            respPlaceInComeStream.setPreferentialType(tempPre);
                            respPlaceInComeStream.setBirdEggAmount(payItem.getBirdEggAmount());//鸟蛋金额
                        }else{
                            if(payItem.getAmount() != null)
                                respPlaceInComeStream.setActualPayAmount(respPlaceInComeStream.getActualPayAmount().add(payItem.getAmount()));  //实际支付
                        }
                        String payType = respPlaceInComeStream.getPayType() + payItem.getPayType() + "/"; //支付方式
                        respPlaceInComeStream.setPayType(payType); //支付方式
                    }
                }
                if(respPlaceInComeStream.getUserType().intValue() == 0 || respPlaceInComeStream.getUserType().intValue() == 1 || respPlaceInComeStream.getUserType().intValue() == 2  || respPlaceInComeStream.getUserType().intValue() == 3)
                    respPlaceInComeStream.setUserType(1);

                if(respPlaceInComeStream.getMemberAmount() == null)
                    respPlaceInComeStream.setMemberAmount(new BigDecimal(0));

                BigDecimal debit = respPlaceInComeStream.getDebit().subtract(respPlaceInComeStream.getMemberAmount()).subtract(respPlaceInComeStream.getPreferentialAmount()).subtract(respPlaceInComeStream.getBirdEggAmount());
                respPlaceInComeStream.setDebit(debit);

                if(respPlaceInComeStream.getCardType() != null){
                    if(respPlaceInComeStream.getCardType() == 4){//次卡
                        if(respPlaceInComeStream.getCardAmount() == null)
                            respPlaceInComeStream.setCardAmount(new BigDecimal(0));

                        BigDecimal tm = respPlaceInComeStream.getCardAmount().divide(new BigDecimal(respPlaceInComeStream.getCardNumber()),10, RoundingMode.HALF_DOWN);
                        if(respPlaceInComeStream.getOpCardCount() != null){
                            tm = tm.multiply(new BigDecimal(respPlaceInComeStream.getOpCardCount()));
                        }
                        respPlaceInComeStream.setMemberAmount(tm);
                    }
                }

            }

        }
        if(pageIndex > page.getTotalPage()){
            lstResult = new ArrayList<>();
            page.setTotalPage(0);
            page.setTotalResult(0);
        }
        logger.info("结束查询场地收入流水---》{}",lstResult.toString());
        PageObj<List<RespPlaceInComeStream>> result = PageObj.create(page.getTotalResult(), page.getCurrentPage(), page.getShowCount(), lstResult);
        return APIResponse.returnSuccess(result);
    }
}
